﻿using Sqlite.Core.Demo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Sqlite.Core.Demo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /* 第一步，安装 Microsoft.EntityFrameworkCore.Sqlite  6.0.26
         * 第二步，创建实体和DbContext子类
         * 第三步，安装 Microsoft.EntityFrameworkCore.Tools 6.0.26
         * 第四步，在程序包管理器控制台执行下列命令
         * Add-Migration InitialCreate
         * Update-Database
         * 
         * 注意事项：Sqlite数据文件的地址因为权限问题，最好放到下面这个地方，
         * var folder = Environment.SpecialFolder.LocalApplicationData;
         * var path = Environment.GetFolderPath(folder);
         * DbPath = System.IO.Path.Join(path, "Application.db");
         * 
         * 否则，使用Update-Database之后，将生成的DB文件覆盖到程序根目录
         * DbPath = System.IO.Path.Join(Environment.CurrentDirectory, "Application.db");
         * 
         * 
         */
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ApplicationDbContext context = new ApplicationDbContext();
            User user = new User();
            user.Name = "杨过";
            user.Email = DateTime.Now.ToString();
            user.Password = "12345678";
            context.Add(user);
            int count =context.SaveChanges();
            //MessageBox.Show(count > 0 ? "添加成功" : "执行失败");
            //MessageBox.Show(context.Users.Count().ToString());
            List<User> users = context.Users.ToList();
            listbox.ItemsSource = users;
        }
    }
}
